package com.bruce.structure.queue;

/**
 * @author luotuan
 * @Description
 * @create 2021-04-21 15:53
 **/
public class LoopQueue {
    private int[] arr;
    private int front;
    private int rear;

    public LoopQueue(int catpactity) {
        this.arr = new int[catpactity];
    }

    private void enQueue(int data) throws Exception {
        if ((rear + 1) % arr.length == front) {
            throw new Exception("队列已满");
        }
        arr[rear] = data;
        rear = (rear + 1) % arr.length;
    }

    private int deQueue() throws Exception {
        if (rear == front) {
            throw new Exception("队列为空");
        }
        int result = arr[front];
        front = (front + 1) % arr.length;
        return result;
    }

    public void output() {
        for (int i = front; i != rear; i = (i + 1) % arr.length) {
            System.out.print(arr[i] + ",");
        }
    }

    public static void main(String[] args) throws Exception {
        LoopQueue loopQueue = new LoopQueue(12);
        loopQueue.enQueue(1);
        loopQueue.enQueue(11);
        loopQueue.enQueue(3);
        loopQueue.enQueue(14);
        loopQueue.enQueue(6);
        loopQueue.deQueue();
        loopQueue.deQueue();
        loopQueue.deQueue();
        loopQueue.output();
    }
}
